package com.bianguo.android.iocdemo.view.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.bianguo.android.iocdemo.R;

public class LoadView extends View {

    private Paint mPaint;
    private PathMeasure pathMeasure, hookMeasure, forkMeasure;
    private Path mPath, hookPath, forkPath;
    private ValueAnimator animator, hookAnim, forkAnim;
    private float stopD, hookStopD, forkStopD, sweepAngle;
    private RectF mRectF;

    public LoadView(Context context) {
        this(context, null);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadView);
        int color = typedArray.getColor(R.styleable.LoadView_color, Color.RED);
        typedArray.recycle();

        mPaint = new Paint();
        mPaint.setStrokeWidth(5);
        mPaint.setColor(color);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        pathMeasure = new PathMeasure();
        mPath = new Path();
        mPath.addCircle(300, 400, 100, Path.Direction.CW);

        hookPath = new Path();
        hookPath.moveTo(300 - 50, 400);
        hookPath.lineTo(300, 400 + 50);
        hookPath.lineTo(300 + 50, 400 - 50);

        forkPath = new Path();
        forkPath.moveTo(300 - 50, 400 - 50);
        forkPath.lineTo(300 + 50, 400 + 50);
        forkPath.moveTo(300 + 50, 400 - 50);
        forkPath.lineTo(300 - 50, 400 + 50);

        pathMeasure.setPath(mPath, true);
        float length = pathMeasure.getLength();
        animator = ValueAnimator.ofFloat(0, length);
        animator.setDuration(1000);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                stopD = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                hookAnim.start();
//                forkAnim.start();
            }
        });
        animator.start();

        hookMeasure = new PathMeasure(hookPath, false);
        hookAnim = ValueAnimator.ofFloat(0, hookMeasure.getLength());
        hookAnim.setDuration(1000);
        hookAnim.setInterpolator(new LinearInterpolator());
        hookAnim.addUpdateListener(animation -> {
            hookStopD = (float) animation.getAnimatedValue();
            invalidate();
        });

        forkMeasure = new PathMeasure(forkPath, false);
        forkAnim = ValueAnimator.ofFloat(0, forkMeasure.getLength());
        forkAnim.setDuration(1000);
        forkAnim.setInterpolator(new LinearInterpolator());
        forkAnim.addUpdateListener(animation -> {
            forkStopD = (float) animation.getAnimatedValue();
            invalidate();
        });

        mRectF = new RectF(300, 300, 500, 500);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawPoint(300, 400, mPaint);
        Path dstPath = new Path();
        pathMeasure.getSegment(0, stopD, dstPath, true);
        canvas.drawPath(dstPath, mPaint);

        Path dstHookPath = new Path();
        hookMeasure.getSegment(0, hookStopD, dstHookPath, true);
        canvas.drawPath(dstHookPath, mPaint);

//        Path dstForkPath = new Path();
//        forkMeasure.getSegment(0, forkStopD, dstForkPath, true);
//        canvas.drawPath(dstForkPath, mPaint);
//        canvas.drawArc(mRectF, 0, sweepAngle, true, mPaint);
    }
}
